package com.yjw.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yjw.pojo.Goodinfo;
import com.yjw.pojo.Goods;
import com.yjw.pojo.Member;
import com.yjw.service.IGoodinfoService;
import com.yjw.service.IGoodsService;
import com.yjw.service.IMemberService;
import com.yjw.utils.DataResults;
import com.yjw.utils.DateTimeUtils;
import com.yjw.utils.ResultCode;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RestController
@Slf4j
@RequestMapping("/goodinfo")
public class GoodsInfoController {

    @Resource
    IGoodinfoService iGoodinfoService;

    @Resource
    IMemberService iMemberService;

    @Resource
    IGoodsService iGoodsService;

    @GetMapping("list")
    public DataResults list(){
        List<Goodinfo> list = iGoodinfoService.list();
        return DataResults.success(ResultCode.SUCCESS,list);
    }

    /**
     * 分页查询商品销售信息
     * @param pageSize
     * @param pageNumber
     * @param goodsId
     * @param memberid
     * @return
     */
    @GetMapping("queryPage")
    public Map<String,Object> queryPage(Integer pageSize,Integer pageNumber,Integer goodsid,Integer memberid){
        Map<String,Object> resultMap = new HashMap<>();


        QueryWrapper<Goodinfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(goodsid != null,"goodsid",goodsid);
        queryWrapper.eq(memberid != null,"memberid",memberid);
        queryWrapper.eq("del",0);

        IPage<Goodinfo> page = iGoodinfoService.page(new Page<Goodinfo>(pageNumber, pageSize), queryWrapper);
        List<Goodinfo> records = page.getRecords();

        for(Goodinfo goodinfo : records){
            Integer memberid1 = goodinfo.getMemberid();
            Member member = iMemberService.getById(memberid1);
            goodinfo.setMember(member);

            Integer goodsid1 = goodinfo.getGoodsid();
            Goods goods = iGoodsService.getById(goodsid1);
            goodinfo.setGoods(goods);
        }

        resultMap.put("total",page.getTotal());
        resultMap.put("rows",records);


        return resultMap;
    }

    /**
     * 新增商品的消费记录
     * @param goodinfo
     * @return
     */
    @PostMapping("add")
    public DataResults add(Goodinfo goodinfo){
        goodinfo.setDel(0);
        goodinfo.setCreatetime(DateTimeUtils.nowTime());
        //编写消费的业务代码
        iGoodsService.sell(goodinfo);
        return DataResults.success(ResultCode.SUCCESS);
    }

    /**
     * 删除指定的消费记录
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public DataResults delete(@PathVariable("id")Integer id){
        Goodinfo goodinfo = new Goodinfo(id,1);
        // update goodinfo set del = 1 where id = ?
        boolean b = iGoodinfoService.updateById(goodinfo);
        if (b){
            return DataResults.success(ResultCode.SUCCESS);
        }
        return DataResults.success(ResultCode.FAIL);
    }

    /**
     * 批量删除消费记录
     * @param array
     * @return
     */
    @DeleteMapping("dellist")
    public DataResults dellist(String array){
        log.info("传递的批量数据是:" + array);
        String[] ids = array.split(",");
        List<Goodinfo> list = new ArrayList<>();
        for (String id : ids){
            list.add(new Goodinfo(Integer.valueOf(id), 1));

        }
        iGoodinfoService.updateBatchById(list);

        return DataResults.success(ResultCode.SUCCESS);
    }
}
